<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品分类" >
        <el-cascader style="width: 800px"  size="small"
                     v-model="categorizeArr" @change="changeProductCategorize"
                     :options="$store.state.factorDict.productCategorizeDict0"
                     :props="{value:'code',label:'chineseName' ,checkStrictly: true }"
                     clearable></el-cascader>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-select v-model="queryParams.productId" placeholder="请选择产品" clearable size="small">
          <el-option v-for="dict in productInfoOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品批次" prop="productBatch">
        <el-input  v-model="queryParams.productBatch" placeholder="请输入产品批次"
          clearable  size="small"  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属企业" prop="enterpriseId">
        <el-cascader  v-model="enterpriseIdArr"  size="small"
                      :options="$store.state.factorDict.enterpriseDict" :show-all-levels="false"
                      :props="{value:'id',label:'enterpriseName',children:'children',checkStrictly:true,}"
                      clearable></el-cascader>
      </el-form-item>
      <el-form-item label="核算日期">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['carbon:productCarbon:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carbon:productCarbon:remove']"
        >删除</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="productCarbonList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="分类" align="center" >
        <template slot-scope="scope">
          {{selectDictLabel($store.state.factorDict.productCategorizeDict0, scope.row.level0,'code','chineseName')}}
          {{scope.row.level1?' / ':''}}
          {{selectDictLabel($store.state.factorDict.productCategorizeDict1, scope.row.level1,'code','chineseName')}}
          {{scope.row.level2?' / ':''}}
          {{selectDictLabel($store.state.factorDict.productCategorizeDict2, scope.row.level2,'code','chineseName')}}
          {{scope.row.level3?' / ':''}}
          {{selectDictLabel($store.state.factorDict.productCategorizeDict3, scope.row.level3,'code','chineseName')}}
        </template>
      </el-table-column>
<!--      <el-table-column label="门类" align="center" prop="level0" />-->
<!--      <el-table-column label="大类" align="center" prop="level1" />-->
<!--      <el-table-column label="中类" align="center" prop="level2" />-->
<!--      <el-table-column label="小类" align="center" prop="level3" />-->
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="所属企业" align="center" prop="enterpriseId" >
        <template slot-scope="scope">
          {{selectDictLabel($store.state.factorDict.enterpriseOptions, scope.row.enterpriseId,'id','enterpriseName')}}
        </template>
      </el-table-column>
      <el-table-column label="产品批次" align="center" prop="productBatch"/>
      <el-table-column label="碳排放核算量" align="center" prop="carbonAccountQuantity" width="120"/>
      <el-table-column label="核算日期" align="center" prop="adjustTime" width="120">
      <template slot-scope="scope">
        <span>{{ parseTime(scope.row.adjustTime,'{y}-{m}-{d}') }}</span>
      </template>
    </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['carbon:productCarbon:edit']"
          >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-setting" @click="handleConsumables(scope.row)">碳核算</el-button>
          <el-button size="mini" type="text" icon="el-icon-setting" @click="calCarbonQuantity(scope.row)">计算</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <change-form ref="changeFormRef" @getList="getList" />

    <product-carbon-stage-form ref="ProductCarbonStageFormRef" @getList="getList"/>
  </div>
</template>

<script>
import { listProductCarbon, getProductCarbon, delProductCarbon, addProductCarbon, updateProductCarbon, exportProductCarbon,calCarbonQuantityById } from "@/api/carbon/productCarbon";
import { listProductInfo } from "@/api/factor/productInfo";
import ChangeForm from "./children/changeForm"
import ProductCarbonStageForm from "./children/ProductCarbonStageForm"
export default {
  name: "ProductCarbon",
  components:{
    ChangeForm,ProductCarbonStageForm
  },
  data() {
    return {
      //产品库
      productInfoOptions:[],
      // 日期范围
      dateRange: [],
      enterpriseIdArr:[],
      categorizeArr:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品碳足迹表格数据
      productCarbonList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        level0: null,
        level1: null,
        level2: null,
        productId: null,
        level3: null,
        productName: null,
        enterpriseId: null,
        productBatch: null,
        adjustTime: null,
        carbonAccountQuantity: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    calCarbonQuantity(row){
      this.$confirm('是否确认计算碳排放核算量?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return calCarbonQuantityById(row.id);
      }).then(() => {
        this.getList();
        this.msgSuccess("计算成功");
      }).catch(function() {});

    },
    handleConsumables(row){
      this.$refs.ProductCarbonStageFormRef.handleShow(row);
    },
    changeProductCategorize(){
      let params={};
      if(this.categorizeArr.length>0){
        params.level0 = this.categorizeArr[0]
      }
      if(this.categorizeArr.length>1){
        params.level1 = this.categorizeArr[1]
      }
      if(this.categorizeArr.length>2){
        params.level2 = this.categorizeArr[2]
      }
      if(this.categorizeArr.length>3){
        params.level3 = this.categorizeArr[3]
      }
      listProductInfo(params).then(response => {
        this.productInfoOptions = response.rows;
      });
    },
    /** 查询产品碳足迹列表 */
    getList() {
      if(this.enterpriseIdArr.length>0){
        this.queryParams.enterpriseId = this.enterpriseIdArr[this.enterpriseIdArr.length-1];
      }else {
        this.queryParams.enterpriseId = null;
      }
      if(this.categorizeArr.length>0){
        this.queryParams.level0 = this.categorizeArr[0]
      }else {
        this.queryParams.level0 = null;
      }
      if(this.categorizeArr.length>1){
        this.queryParams.level1 = this.categorizeArr[1]
      }else {
        this.queryParams.level1 = null;
      }
      if(this.categorizeArr.length>2){
        this.queryParams.level2 = this.categorizeArr[2]
      }else {
        this.queryParams.level2 = null;
      }
      if(this.categorizeArr.length>3){
        this.queryParams.level3 = this.categorizeArr[3]
      }else {
        this.queryParams.level3 = null;
      }
      listProductCarbon(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.productCarbonList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.enterpriseIdArr=[];
      this.categorizeArr=[];
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.changeFormRef.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.changeFormRef.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProductCarbon(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
  }
};
</script>
